package com.hxzy.common.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.Base64;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Base64Utils {
    /**
     * base64转图片生成文件路径
     *
     * @param imgStr
     * @param imgFilePath
     * @return
     */
    public static boolean base64ToImage(String imgStr, String imgFilePath) {
        // 图像数据为空
        if (null == imgStr || "".equals(imgStr.trim())) {
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }

    /**
     * base64转图片生成文件流(需要自己关闭流)
     *
     * @param imgStr
     * @param out
     * @return
     */
    public static boolean base64ToImage(String imgStr, OutputStream out) {
        if (null == imgStr || "".equals(imgStr.trim())) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }


    /**
     * 文件路径转base64
     *
     * @param imgFile
     * @return
     */
    public static String imageToBase64(String imgFile) {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串

    }

    /**
     * 图片文件流转字符串base64
     *
     * @param fileInputStream
     * @return
     */
    public static String imageToBase64(InputStream fileInputStream) {
        if (fileInputStream == null) {
            return null;
        }
        byte[] data = null;
        // 读取图片字节数组
        try {
            data = new byte[fileInputStream.available()];
            fileInputStream.read(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }


    // 对字节数组字符串进行Base64解码并-->生成图片
    public static boolean GenerateImage(String imgStr, String imgFilePath) {

        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
//          byte[] sdsd =   Base64.getDecoder().decode(imgStr);
            imgStr = imgStr.replaceAll("\r\n","");
            imgStr = URLDecoder.decode(imgStr,"utf-8");
            // Base64解码
            byte[] bytes = null; //decoder.decodeBuffer(imgStr);

//            byte[] b =new byte[0] ;
            if(imgStr.contains("data:image/png;base64")){
                //Base64解码
                bytes=decoder.decodeBuffer(imgStr.replace("data:image/png;base64,", ""));
            }
            if(imgStr.contains("data:image/jpeg;base64")){
                //Base64解码
                bytes=decoder.decodeBuffer(imgStr.replace("data:image/jpeg;base64,", ""));
            }
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
